package com.topscore.uke.ui.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.topscore.uke.R;
import com.topscore.uke.ui.BaseSupportFragment;
import com.topscore.uke.ui.widgets.DividerItemDecoration;
import com.topscore.uke.utils.DisplayImageOptionsManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProductDetailFragment extends BaseSupportFragment {

    @Bind(R.id.root_recycler)
    RecyclerView rootRecycler;
    private View rootView;

    private BaseQuickAdapter<String, BaseViewHolder> mRecyclerAdapter
            = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.product_detail_image_item, new ArrayList<>()) {
        @Override
        protected void convert(BaseViewHolder holder, String item) {
            ImageView imageView = holder.getView(R.id.image_iv);
            ImageLoader.getInstance().displayImage(item, imageView, DisplayImageOptionsManager.getInstance().getDefaultOptions());
        }
    };
    private final String[] mImages = {"http://ios-setup.oss-cn-beijing.aliyuncs.com/UkeImage_test/bannerOne@2x.png",
            "http://ios-setup.oss-cn-beijing.aliyuncs.com/UkeImage_test/bannerTwo@2x.png",
            "http://ios-setup.oss-cn-beijing.aliyuncs.com/UkeImage_test/bannerThree@2x.png",
            "http://ios-setup.oss-cn-beijing.aliyuncs.com/UkeImage_test/bannerFour@2x.png"};

    public ProductDetailFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_product_detail, container, false);
        ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    private void initViews() {
        RecyclerView.LayoutManager layoutManager;
        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
//        layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setAutoMeasureEnabled(true);
        rootRecycler.setLayoutManager(layoutManager);
        rootRecycler.addItemDecoration(new DividerItemDecoration(mContext, R.drawable.divider_transparent));
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, (ViewGroup) rootRecycler.getParent(), false);
        mRecyclerAdapter.setEmptyView(emptyView);
        rootRecycler.setAdapter(mRecyclerAdapter);
//        initData();
    }

//    private void initData() {
//        List<String> images = new ArrayList<>();
//        for (int i = 0; i < mImages.length; i++) {
//            images.add(mImages[i]);
//        }
//        mRecyclerAdapter.setNewData(images);
//    }

    public void setUpImageData(List<String> imageData) {
        mRecyclerAdapter.setNewData(imageData);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
